<?php
class AreaModel extends MY_Model {

    function __construct()
    {
        parent::__construct();
        $this->_table = 'area';
    }
	
	function insert($data) {
		
		try {
			$count = $this->count(array('name'=>$data['name']));
			if($count > 0)
				return -1;
			$this->db->trans_strict(FALSE);
			$id = parent::insert($data);
			$position = $id;
			if ($data['parentid']) {//parentid != 0
    			$parent = $this->selectById($data['parentid']);
    			$position = $parent['position'].','.$id;
    		}
    		$this->update_position($id, $position);
	    	$this->db->trans_complete();
		} catch (Exception $e) {
			$this->db->trans_rollback();
			return 0;
		}
//		if ($this->db->trans_status() === FALSE){
//		    // 生成一条错误信息... 或者使用 log_message() 函数来记录你的错误信息
//		    
//		}
		return $id;
    }
    
    function update_position($id,$position) {
    	$data['id'] = $id;
    	$data['position'] = $position;
    	$this->db->where('id', $id);
    	return $this->db->update($this->_table,$data);
    }
}
?>